/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.domain;

import java.util.HashMap;

public class Unit {
    private String unit;
    private Float quantity;
    private String txtQuantity;
    private final static HashMap<String, Float> text2Digital;
    
    static {
        text2Digital = new HashMap<String, Float>();
        text2Digital.put("一", new Float(1));
        text2Digital.put("二", new Float(2));
        text2Digital.put("三", new Float(3));
        text2Digital.put("四", new Float(4));
        text2Digital.put("五", new Float(5));
        text2Digital.put("六", new Float(6));
        text2Digital.put("七", new Float(7));
        text2Digital.put("八", new Float(8));
        text2Digital.put("九", new Float(9));
        text2Digital.put("十", new Float(10));
        text2Digital.put("半", new Float(0.5));
    }
    
    public Unit() {
        unit = "";
        quantity = 0f;
        txtQuantity = "";
    }
    
    public Unit(String quantity,String unit) {
        this.unit = unit;
        this.txtQuantity = quantity;
        this.quantity = Transform(quantity);
    }
    
    private float Transform(String q) {
        float dq = 0;
        if (q != null) {
            for (int i = 0; i < q.length(); i ++) {
                char t = q.charAt(i);
                float d = text2Digital.get(t + "");
                if (d == 10f) {
                    if (dq==0) {
                        dq = 1;
                    }
                    dq = dq * 10; 
                } else {
                    dq = dq + d;
                }
            }
        }
        return dq;
    }

    public Unit(String unit, float quantity) {
        this.unit = unit;
        this.quantity = quantity;
    }
    
    public float getQuantity() {
        return quantity;
    }
    public void setQuantity(float quantity) {
        this.quantity = quantity;
    }
    public String getUnit() {
        return unit;
    }
    public void setUnit(String unit) {
        this.unit = unit;
    }
    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((unit == null) ? 0 : unit.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Unit other = (Unit) obj;
        if (unit == null) {
            if (other.unit != null)
                return false;
        } else if (!unit.equals(other.unit))
            return false;
        return true;
    }
    
    public String getHtml(boolean original) {
        StringBuffer sb = new StringBuffer();
        sb.append("<td>");
        if (original) {
            sb.append(this.txtQuantity);
        } else {
            sb.append(this.quantity);
        }
        sb.append("</td>");
        sb.append("<td>");
        sb.append(this.unit);
        sb.append("</td>");
        return sb.toString();
    }

    public String getTxtQuantity() {
        return txtQuantity;
    }

    public void setTxtQuantity(String txtQuantity) {
        this.txtQuantity = txtQuantity;
    }

    public void setQuantity(Float quantity) {
        this.quantity = quantity;
    }

}